package com.kczd.jinlan.view.shouye.classActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ExpandableListView;

import com.kczd.jinlan.R;
import com.kczd.jinlan.adapter.MyExpandableListViewAdapter;
import com.kczd.jinlan.model.ClassBaby;
import com.kczd.jinlan.model.Classify;
import com.kczd.jinlan.utils.Config;
import com.kczd.jinlan.view.shouye.fragment.ShangchengFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * 8个按钮展示页面
 */
public class ClassifydetilActivity extends AppCompatActivity {

    private static final String TAG="ClassifyActivity.class";
    String url = Config.net_addr + "index/category";
    private String grade_id;

    public static List<Classify> minelist;

    /**
     * 所有分组的所有子项的 GridView 数据集合
     */
    private List<List<String>> itemList = new ArrayList<>();

    /**
     * 所有分组的所有子项的 GridView 数据集合
     */
    public static List<List<String>> itemListid=new ArrayList<>();
    /*
      数据封装ExpandableListView
    */
    private ExpandableListView expandableListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classifydetil);

        String  position= getIntent().getStringExtra("claseeify_position");
        grade_id= ShangchengFragment.minelist.get(Integer.parseInt(position)).getId();

        HandlerData();
    }
    private void HandlerData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                getServiceData();
            }
        }).start();
    }
    public void getServiceData() {
        //创建okHttpClient对象
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody=new FormBody.Builder()
                .add("id",grade_id)
                .build();
        Request request=new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try {
            Response response=client.newCall(request).execute();
            String strResult=response.body().string();
            Message msg = new Message();
            Bundle data = new Bundle();
            data.putString("ret_json", strResult);
            msg.setData(data);
            handler.sendMessage(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle data = msg.getData();
            String val = data.getString("ret_json");
            val = val.substring(val.indexOf("{"),val.lastIndexOf("}")+1);
            Log.e(TAG,val);
            JSONObject jsonResult;
            try {
                jsonResult = new JSONObject(val);
                // UI界面的更新等相关操作
                if(Config.code.equals(jsonResult.getString("code"))){
                    JSONArray js=jsonResult.getJSONArray("data");
                    minelist = new ArrayList<Classify>();
                    for(int i=0;i<js.length();i++)
                    {
                        List<String> list=new ArrayList<>();
                        List<ClassBaby> list1=new ArrayList<>();
                        Classify classify=new Classify();
                        JSONObject jbi=js.getJSONObject(i);
                        classify.setId(jbi.getString("id"));
                        classify.setImage(jbi.getString("image"));
                        classify.setName(jbi.getString("name"));
                        classify.setMobile_name(jbi.getString("mobile_name"));
                        classify.setParent_id(jbi.getString("parent_id"));
                        classify.setParent_id_path(jbi.getString("parent_id_path"));
                        classify.setLevel(jbi.getString("level"));
                        classify.setSort_order(jbi.getString("sort_order"));
                        classify.setIs_show(jbi.getString("is_show"));
                        classify.setIs_hot(jbi.getString("is_hot"));
                        classify.setCat_group(jbi.getString("cat_group"));
                        JSONArray js1=jbi.getJSONArray("sub_menu");
                        for(int j=0;j<js1.length();j++)
                        {
                            ClassBaby classBaby=new ClassBaby();
                            JSONObject jbs=js1.getJSONObject(j);
                            String name=jbs.getString("name");
                            classBaby.setName(name);
                            list.add(name);
                            String id=jbs.getString("id");
                            classBaby.setId(id);
                            list1.add(classBaby);
                        }
                        classify.setClassbaby(list1);
                        minelist.add(classify);
                        itemList.add(list);
                    }
                    init();
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };


    private void init() {
        expandableListView = (ExpandableListView) findViewById(R.id.list_view_expandable);
        expandableListView.setDivider(null);//去除listview的下划线

        // 创建适配器
        MyExpandableListViewAdapter adapter = new MyExpandableListViewAdapter(ClassifydetilActivity.this, minelist, itemList);
        expandableListView.setAdapter(adapter);
        // 隐藏分组指示器
        expandableListView.setGroupIndicator(null);
        // 默认展开第一组
        expandableListView.expandGroup(0);
    }

}
